<?php
/**
 * 前台control父类,会员control父类
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');

class Control{
	

	protected function __construct(){
			

	}


	/**
	 * 验证会员是否登录
	 *
	 */
	protected function checkLogin(){

		if ($_SESSION['is_login'] !== '1'&&$_GET['ac']!=='login'&&$_GET['ac']!=='seccode'){
		
			$ref_url = request_uri();

			@header("location: index.php?ac=login&ref_url=".urlencode($ref_url));
			exit;
		}
	}

	/**
	 * 检查短消息数量
	 *
	 */
	protected function checkMessage() {
		if($_SESSION['member_id'] == '') return ;
		//判断cookie是否存在
		$cookie_name = 'msgnewnum'.$_SESSION['member_id'];
		if (cookie($cookie_name) != null && intval(cookie($cookie_name)) >=0){
			$countnum = intval(cookie($cookie_name));
		}else {
			$message_model = Model('message');
			$countnum = $message_model->countNewMessage($_SESSION['member_id']);
			setNcCookie($cookie_name,"$countnum",2*3600);//保存1天
		}
		Tpl::output('message_num',$countnum);
	}

}

/********************************** 前台control父类 **********************************************/

class BaseHomeControl extends Control {

	public function __construct(){
		//短消息检查
		//$this->checkMessage();
		//购物车商品种数查询
		//$this->queryCart();
		//验证是否登录
		$this->checkLogin();

		Language::read('common,home_layout');

		Tpl::setDir('home');

		Tpl::output('nav_list',($nav = H('nav'))? $nav :H('nav',true));
		Tpl::setLayout('home_layout');

	}
}



/********************************** 会员control父类 **********************************************/

class BaseMemberControl extends Control {
	public function __construct(){
		
			parent::__construct();
		if(!C('site_status')) halt(C('closed_reason'));

		Language::read('common,member_layout');

		if ($_GET['column'] && strtoupper(CHARSET) == 'GBK'){
			$_GET = Language::getGBK($_GET);
		}
		//会员验证
		$this->checkLogin();
		//短消息检查
		$this->checkMessage();

	}

	/**
	 * 检查店铺名称是否存在
	 * @return boolean
	 */
	public function checknameinner() {
		$model_store	= Model('store');

		$store_name	= trim($_GET['store_name']);
		$store_info	= $model_store->getStoreInfo(array('store_name'=>$store_name));
		if($store_info['store_name'] != ''&&$store_info['member_id'] != $_SESSION['member_id']) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 买家的左侧上部的头像和订单数量
	 *
	 */
	public function get_member_info() {
		//生成缓存的键值
		$hash_key = $_SESSION['member_id'];
		//写入缓存的数据
		$cachekey_arr = array('member_name','store_id','member_avatar','member_qq','member_email','member_ww','member_goldnum','member_points',
							'available_predeposit','member_snsvisitnum','credit_arr','order_nopay','order_noreceiving','order_noeval','fan_count');
		if (false){
			foreach ($_cache as $k=>$v){
				$member_info[$k] = $v;
			}
		} else {
		    $model_order = Model('order');
		    $model_member = Model('member');
		    $member_info = $model_member->getMemberInfo(array('member_id'=>$_SESSION['member_id']));
			$member_info['order_nopay'] = $model_order->getOrderStateNewCount(array('buyer_id'=>$_SESSION['member_id']));
			$member_info['order_noreceiving'] = $model_order->getOrderStateSendCount(array('buyer_id'=>$_SESSION['member_id']));
			$member_info['order_noeval'] = $model_order->getOrderStateEvalCount(array('buyer_id'=>$_SESSION['member_id']));
		}
		Tpl::output('member_info',$member_info);
		Tpl::output('header_menu_sign','snsindex');//默认选中顶部“买家首页”菜单
	}
}


/********************************** 前台彩票类型父类 **********************************************/

class BaseLotteryHallControl extends Control {

	public function __construct(){
		$this->checkLogin();

		Tpl::setDir('lotteryHall');

		Tpl::setLayout('null_layout');

	}

	//随机函数
	public function randomkeys($length)
	{
	 $key = "";
	 $pattern='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	 $pattern1='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	 $pattern2='0123456789';
	 for($i=0;$i<$length;$i++)
	 {
	   $key .= $pattern{mt_rand(0,35)};
	 }

	 return $key;
	}

}


/********************************** 前台彩票团队父类 **********************************************/

class BaseTeamControl extends Control {

	public function __construct(){
		$this->checkLogin();

		Tpl::setDir('team');

		Tpl::setLayout('null_layout');

	}

	//随机函数
	public function randomkeys($length)
	{
	 $key = "";
	 $pattern='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	 $pattern1='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	 $pattern2='0123456789';
	 for($i=0;$i<$length;$i++)
	 {
	 	$key .= $pattern{mt_rand(0,35)};
	 }

	 return $key;
	}

}